/*
 * @Author: HX
 * @Date: 2024-12-17 12:02:45
 * @LastEditors: HX
 * @LastEditTime: 2024-12-17 14:12:36
 */
import { vector3Offset } from './common.js';
export const PersonControls = {
  /**
   * @description: 是否允许鼠标输入修改相机位置和方向，默认：true  true/false都试试
   * @param {*} flag
   * @return {*}
   */
  resetInputs(flag) {
    let scene = GlobalViewer.scene;
    let camera = scene.mainCamera;
    let cameraController = camera.cameraController();
    cameraController.enableInputs = flag;
  },
  /**
   * @description: 镜头移动
   * @param {*} radian //移动角度heading： 0:前方,Math.PI / 2: 右 ...
   * @param {*} direction //移动角度pitch 无视
   * @param {*} speed //移动速度
   * @return {*}
   */
  translate(radian, direction, speed = 2) {
    let scene = GlobalViewer.scene;
    let camera = scene.mainCamera;
    let position = camera.transform.position;
    let cameraController = camera.cameraController();
    let heading = cameraController.heading; //朝向
    let pitch = cameraController.pitch; //俯仰角
    if (direction == 's') {
      pitch = -pitch;
    }

    radian += heading;

    camera.transform.position = vector3Offset(position, {
      offsetX: Math.sin(radian) * speed,
      offsetY: Math.cos(radian) * speed,
      offsetZ: direction ? Math.sin(pitch) * speed : 0
    });
  },
  /**
   * @description: 镜头旋转  只管left
   * @param {*} left  //heading
   * @param {*} up  //pich
   * @param {*} speed //速度
   * @return {*}
   */
  rotate(left = 0, up = 0, speed = 800) {
    let scene = GlobalViewer.scene;
    let camera = scene.mainCamera;
    let position = camera.transform.position;
    let cameraController = camera.cameraController();
    let heading = cameraController.heading; //朝向
    let pitch = cameraController.pitch; //俯仰角
    let roll = cameraController.roll; //翻滚角

    let delta = Math.PI / 4 / speed; //默认800px旋转45度
    let rotateLeft = left * delta;
    let rotateUp = up * delta; //俯仰角

    heading += rotateLeft;
    pitch += rotateUp;


    cameraController.setView(position.toCartesian3(), heading, pitch, roll);
  }
};
